package com.lv.common.utils

import android.content.Context
import com.alibaba.sdk.android.oss.ClientConfiguration
import com.alibaba.sdk.android.oss.OSS
import com.alibaba.sdk.android.oss.OSSClient
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider


/**
 * 作者： ygx
 * 创建日期：2024/9/21 2:53 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class AliOssUtils {
    companion object {
        private var instance: AliOssUtils? = null
        var oss: OSS? = null
        fun getInstance(): AliOssUtils? {
            if (instance == null) {
                instance = AliOssUtils()
            }
            return instance
        }
    }

    private var mOSSClient: OSSClient? = null
    fun getOSS(mContext: Context): OSSClient? {
        if (mOSSClient == null) {
            // 推荐使用OSSAuthCredentialsProvider，Token过期后会自动刷新。
            val stsServer = AliUpLoadConfig.STS_SERVER_URL
            val credentialProvider: OSSCredentialProvider = OSSAuthCredentialsProvider(stsServer);
            // 完成以下配置项。
            // 创建OSSClient实例。
            val conf = ClientConfiguration()
            conf.connectionTimeout = 60 * 1000 // 连接超时，默认15秒
            conf.socketTimeout = 60 * 1000 // socket超时，默认15秒
            conf.maxConcurrentRequest = 5 // 最大并发请求书，默认5个
            conf.maxErrorRetry = 2 // 失败后最大重试次数，默认2次
            mOSSClient = OSSClient(mContext, AliUpLoadConfig.OSS_ENDPOINT, credentialProvider, conf)
//        OSSLog.enableLog()
//        return OssService(oss, editBucketName, displayer)
            return mOSSClient
        } else {
            return mOSSClient
        }

    }
}